/*
app.factory('', function () {
    var data = {};
    this.setData = function (newData) {
        data = newData;
    };
    this.getData = function (){
        return data
    }
});
*/
app.service('serv',function () {
    var data = {};
    this.setData = function (newData) {
        data = newData;
    };
    this.getData = function (){
        return data
    }
});

app.controller('paichaController',['$scope','$http','$rootScope','serv',function($scope,$http,$rootScope,serv){
    var token = localStorage.getItem('token');
    var datas = [];
    mui.showLoading('正在加载','div');
    // mui.hideLoading();
    $http({
        method: "POST",
        url: mainUrl+'/tr/tr-chek-item!find4App.action',
        params: {
            'Authorization': token
            /*'qm.flag' : 1*/
        }
    }).then(
        function(data, status, headers, config){
            console.log(data)
            datas.push(data.data.items);
            $scope.data = data.data.items;
            mui.hideLoading();
            for(var i=0;i<data.data.items.length;i++){
                var sid = data.data.items[i].sid;
                var sidData = data.data.items[i];
                var state = data.data.items[i].state;
                if(state=='01'){
                    localStorage.setItem(sid,JSON.stringify(sidData));
                    console.log(sidData)
                }
            }
            // document.getElementsByClassName('mui-switch-blue').addEventListener
        },function(data, status, headers, config){

        }
    );

    $scope.sending = function ($event){
        serv.setData($event);
    };
    var flag = [];
    $scope.panduan = function ($event) {
        // console.log($event.trFlag);
        var getPanduan = JSON.parse(localStorage.getItem($event.sid));
        // console.log($event.sid);
        // console.log(getPanduan.trFlag)
        if ($event.trFlag != getPanduan.trFlag) {
            flag.push($event)
        } else {
            for (var a = 0; a < flag.length; a++) {
                if (flag[a].sid != undefined && flag[a].sid == $event.sid) {
                    flag.splice(a, 1)
                }
            }
        }
    }
    mui.init();
    // mui("div").switch().toggle();
$scope.submitPc = function () {
    var token = localStorage.getItem('token');
    mui.showLoading('正在加载','div');
console.log(flag)
if(flag.length>0){
    var jsonStr = [];
    var datas = {}
    for(var i=0;i<flag.length;i++){
        datas.sid = flag[i].sid;
        if(flag[i].trFlag == 1){
            datas.trFlag = true
        }else {
            datas.trFlag = false
        }
        datas.remark = '';
        datas.gpsLocation = '';
        datas.version = flag[i].version;
        jsonStr.push(datas)
    }
    console.log(jsonStr);
    $http({
        method: "POST",
        url: mainUrl+'/tr/tr-chek-item!updateTrChekItem4App.action',
        params: {
            'Authorization': token,
             'qm.jsonStr':jsonStr
            /*'qm.flag' : 1*/
        }
    }).then(
        function(data, status, headers, config){
             console.log(data);
            //$scope.data = data.data.items;

            mui.hideLoading();
            window.location.reload();
            // document.getElementsByClassName('mui-switch-blue').addEventListener
        },function(data, status, headers, config){

        }
    );

}else{
    mui.alert('没有数据更改','无法提交','确认');
    mui.hideLoading();
}


    mui.hideLoading();

};

console.log($scope.data)

    $scope.change = function ($event) {
        var display1 = [];
        var display2 = [];
        var display3 = [];
        var display4 = [];
        var display5 = [];
        var display6 = [];


        for(var i=0;i<datas[0].length;i++){
            var unit = datas[0][i].unit;

            //var display7 = [];
            // console.log(unit);
            // console.log($event);
            // console.log(datas[0][i])
            // if($event=='all'){
            //         display.push(datas[0][i]);
            // }
            if(unit=='06'){
                display6.push(datas[0][i]);
            }if(unit=='05'){
                display5.push(datas[0][i]);
            }if(unit=='04'){
                display4.push(datas[0][i]);
            }if(unit=='03'){
                display3.push(datas[0][i]);
            }if(unit=='02'){
                display2.push(datas[0][i]);
            }if(unit=='01'){
                display1.push(datas[0][i]);
            }
            }
        switch ($event){
            case 'all':
                $scope.data = datas[0];
                break;
            case 'day':
                $scope.data = display1;
                break;
            case 'class':
                $scope.data = display2;
                break;
            case 'week':
                $scope.data = display3;
                break;
            case 'month':
                $scope.data = display4;
                break;
            case 'season':
                $scope.data = display5;
                break;
            case 'year':
                $scope.data = display6;
                break;

        }
    }



}]);